<?php

class AdminController extends Zend_Controller_Action {

    /**
     * FlashMessenger
     *
     * @var Zend_Controller_Action_Helper_FlashMessenger
     */
    protected $_flashMessenger = null;

    /**
     * Текущая локаль.
     * @var string
     */
    protected $_locale;

    public function init() {

//        $this->_helper->layout()->setLayout('index-layout');
        $this->_helper->layout()->disableLayout();
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');

        $localeSession = new Zend_Session_Namespace('locale');
        $this->_locale = $localeSession->locale;

        $pagesManager = new \Managers\Pages();
        $menu = $pagesManager->all($this->_locale, true);

        $this->view->menu = $menu;

    }

    public function localesAction() {

        $localesManager = new \Managers\Locales();
        $qb = $localesManager->qb();
        $grid = $this->grid($qb);

        $this->view->form = $grid->deploy();

        $this->renderScript('/grid/form.phtml');
    }

    public function pagesAction() {

        $pagesManager = new \Managers\Pages();
        $qb = $pagesManager->qb();
        $grid = $this->grid($qb);

        $this->view->form = $grid->deploy();

        $this->renderScript('/grid/form.phtml');
    }

    public function translatesAction() {

        $translatesManager = new \Managers\Translates();
        $qb = $translatesManager->qb();
        $grid = $this->grid($qb);

        $this->view->form = $grid->deploy();

        $this->renderScript('/grid/form.phtml');
    }

    public function menuAction() {

        $menusManager = new \Managers\Menu();
        $qb = $menusManager->qb();

        $grid = $this->grid($qb);

        $this->view->form = $grid->deploy();

        $this->renderScript('/grid/form.phtml');

    }

    public function grid($qb, $id = '') {

        $view = new Zend_View();
        $view->setEncoding('UTF-8');

        $config = new Zend_Config_Ini(APPLICATION_PATH . '/grids/grid.ini', 'production');

        $grid = Bvb_Grid::factory('Table', $config, $id);

        $grid->setEscapeOutput(false);
        $grid->setExport(array('csv', 'excel', 'pdf'));
        $grid->setView($view);

        $gridManager = new \Managers\Grid();
        $em = $gridManager->em();
        $source = new Bvb_Grid_Source_Doctrine2($qb, $em);
        $grid->setSource($source);
        $grid->updateColumn('id', array('hidden' => true));

        $form = new Bvb_Grid_Form();
        $form->setAdd(true)->setEdit(true)->setDelete(true)->setAddButton(true);
        $grid->setForm($form);

        return $grid;
    }

}
